/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.model.operate.buffer;

import java.util.ArrayList;
import java.util.List;

import chunmap.model.coord.CoordSeqEditor;
import chunmap.model.coord.Coordinate2D;
import chunmap.model.coord.Position;
import chunmap.model.geom.Ring;
import chunmap.model.geom.Polygon;

/**
 * @author chunquedong
 * 
 */
public class PointBuffer {
	public Polygon createBuffer(Position p, double distance, double pointNum) {

		if (distance <= 0 || pointNum <= 3) {
			throw new IllegalArgumentException(
					"very little pointNum or distance in pointBuffer");
		}
		List<Position> points = new ArrayList<Position>();

		double af = 6.28 / pointNum;
		for (double a = 0; a < 6.28; a += af) {
			double dx = Math.sin(a) * distance;
			double dy = Math.cos(a) * distance;

			Position pp = new Coordinate2D(p.getX() + dx, p.getY() + dy);
			points.add(pp);
		}
		CoordSeqEditor ls = new CoordSeqEditor(points);
		ls.close();
		Polygon pg = new Polygon(new Ring(ls.toCoordinateSeq()));
		return pg;
	}

	public Polygon createBuffer(Position p, double d) {
		return createBuffer(p, d, 20);
	}
}